package com.ycd.dao.Impl;

import com.ycd.Entity.UserEntity;
import com.ycd.dao.XinWenEntityDao;
import com.ycd.Entity.XinWenEntity;
import com.ycd.Util.jdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 新闻的实现类
 * @Author ycd
 * @Date : 2022/3/11 17:28
 * @Version : 1.0
 */
public class XinWenEntityImpl implements XinWenEntityDao {

    /**
     * 添加新闻
     * @param xinwenentity  新闻实现
     */
    @Override
    public void addXinWen(XinWenEntity xinwenentity){
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "insert into news (TITLE,CHNL_CODE,DETAIL,CRTIME,UPTIME) values(?,?,?,now(),now())";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,xinwenentity.getTitle());
            stmt.setString(2,xinwenentity.getCHNL_CODE());
            stmt.setString(3,xinwenentity.getDETAIL());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, null);
        }
    }

    /**
     * 修改用户
     * @param xinwenentity
     */
    @Override
    public void updateXinWen(XinWenEntity xinwenentity){
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = " update news set headLine=?,content=?,deleteFlag=?,issuer=?,img=?,top=?,state=? where id=?";
            stmt = conn.prepareStatement(sql);


            stmt.setInt(8, xinwenentity.getId());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, null);
        }
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void delXinWen(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "delete from news where id="+id+"";
            stmt = conn.prepareStatement(sql);
            stmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ;
    }

    /**
     * 查询所有新闻
     * @return 集合
     */
    @Override
    public List<XinWenEntity> queryAll() {
        List<XinWenEntity> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "select a.id,a.title,a.DETAIL,a.CRTIME,a.UPTIME,b.name from news a,news_chnl b where a.CHNL_CODE=b.id order by a.CRTIME  desc";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                XinWenEntity xinwenentity = new XinWenEntity();
                xinwenentity.setId(rs.getInt("id"));
                xinwenentity.setTitle(rs.getString("title"));
                xinwenentity.setDETAIL(rs.getString("DETAIL"));
                xinwenentity.setCRTIME(rs.getDate("CRTIME"));
                xinwenentity.setUPTIME(rs.getDate("UPTIME"));
                xinwenentity.setCHNL_CODE(rs.getString("name"));
                list.add(xinwenentity);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, rs);
        }
        return list;
    }


    /**
     * 根据ID查新闻
     * @param id
     * @return 一个新闻
     */
    @Override
    public XinWenEntity getUserById(int id) {
        XinWenEntity xinwenentity = null;

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "select id,title,DETAIL,CRTIME,UPTIME from news where where id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            rs = stmt.executeQuery();
            rs.next();
            while(rs.next()){
                xinwenentity=  new XinWenEntity();
                xinwenentity.setId(rs.getInt("id"));
                xinwenentity.setTitle(rs.getString("title"));
                xinwenentity.setDETAIL(rs.getString("DETAIL"));
                xinwenentity.setCRTIME(rs.getDate("CRTIME"));
                xinwenentity.setUPTIME(rs.getDate("UPTIME"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, rs);
        }
        return xinwenentity;
    }

    @Override
    public List<XinWenEntity> queryNewsByChannel(int CHNL_CODE) {
        XinWenEntity xinwenentity = null;
        Connection conn=jdbcUtil.getCon();
        PreparedStatement stmt =null;
        ResultSet rs=null;
        List<XinWenEntity> list =new ArrayList<>();
        try {
            String spl="select *from news where CHNL_CODE";
            stmt=conn.prepareStatement(spl);
            rs=stmt.executeQuery();
            rs.next();
            while(rs.next()){
                xinwenentity=  new XinWenEntity();
                xinwenentity.setId(rs.getInt("id"));
                xinwenentity.setTitle(rs.getString("title"));
                xinwenentity.setDETAIL(rs.getString("DETAIL"));
                xinwenentity.setCRTIME(rs.getDate("CRTIME"));
                xinwenentity.setUPTIME(rs.getDate("UPTIME"));
                list.add(xinwenentity);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, rs);
        }
        return list;
    }

}
